<template>
  <dt-modal-edit ref="editRef" :title="title" @ok="onOk" @cancelData="cancelData">
    <dt-info :label-width="120" ref="proInfo"></dt-info>
  </dt-modal-edit>
</template>

<script setup>
  import { ref } from 'vue'
  import dt from '@dt/dt'
  import { editOpt } from '../biz/addOpt'
  import apiMgr from '../biz/index.js'
  const proInfo = ref()
  const editInfo = ref({
    companyName: '',
    remark: ''
  })

  const emits = defineEmits(['successful'])

  const title = ref('新增合同主体')

  const onOk = () => {
    let params1 = {
      companyName: editInfo.value.companyName,
      id: editInfo.value.id
    }
    apiMgr.getSysCompanyStatusForWeb(params1).then((res) => {
      if (res) {
        dt.ui.Modal.info({
          title: '确认',
          okText: '知道了',
          content: `主体名称“${editInfo.value.companyName}”已被占用，请勿重复创建`
        })
      } else {
        let params = {
          companyName: editInfo.value.companyName,
          remark: editInfo.value.remark
        }
        if (editInfo.value.id) {
          params.id = editInfo.value.id
        }
        apiMgr.saveSysCompanyForWeb(params).then((res) => {
          dt.ui.Message.success('主体新增编辑成功')
          emits('successful')
        })
      }
    })
  }
  const tipsFlag = ref(false)
  const fun = {
    checkPhone: () => {
      let reg = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/
      if (!reg.test(editInfo.value.belongUserPhone)) {
        return dt.ui.Message.warning('请输入正确的手机号')
      } else {
        let params = {
          phone: editInfo.value.belongUserPhone
        }
        apiMgr.getTenantInfoByPhoneForWeb(params).then((res) => {
          userPhoneVal.value = res
          console.log(res, 'res')
          if (!res.registerStatus) {
            dt.ui.Modal.confirm({
              title: '验证手机号',
              content: `该手机号为未注册手机号。<br/>请确认，是否以该手机号作为归属人手机号并创建新用户?`,
              onOk: () => {
                console.log(userPhoneVal.value, 'vvv')
                editInfo.value.phoneVerify = editInfo.value.belongUserPhone
                editInfo.value.belongUserName = userPhoneVal.value.userName
                if (editInfo.value.belongUserName && userPhoneVal.value.registerStatus == 1) {
                  tipsFlag.value = true
                } else {
                  tipsFlag.value = false
                }
              },
              onCancel: () => {
                dt.ui.Message.info('您取消了操作。')
              }
            })
          } else {
            showModal.value = true
          }
        })
      }
    }
  }
  const editRef = ref()
  const cancelData = () => {
    editInfo.value = { companyName: '', remark: '' }
  }
  const hide = () => editRef.value.hide()

  const show = (val) => {
    if (val?.id) {
      editInfo.value.id = val.id
      editInfo.value.companyName = val.companyName
      editInfo.value.remark = val.remark
    }
    editRef.value.show()
    proInfo.value.init(new editOpt(fun), editInfo.value)
  }
  const showModal = ref(false)
  defineExpose({
    show,
    hide
  })
</script>

<style lang="scss" scoped>
  .topBox {
    display: flex;
    flex-direction: row;
    justify-content: space-between;
    border: 1px #ccc solid;
    background: #f7f4f4;
    padding: 20px 20px;
    line-height: 30px;
    font-size: 16px;
  }
  .tips {
    padding: 5px 10px;
    line-height: 1.4;
    font-size: 14px;
    background: rgba(254, 252, 236, 1);
    margin: 10px;

    .tips_icon {
      font-size: 20px;
      color: #0f42a7;
      vertical-align: sub;
    }
  }
  .title {
    position: relative;
    padding-left: 16px;
    margin: 10px 0;
  }
  .title::before {
    content: '';
    width: 8px;
    height: 16px;
    background: #2d61f4;
    position: absolute;
    left: 0;
    top: 4px;
  }
  .formBox {
    display: flex;
    flex-direction: row;
    justify-content: space-between;
    flex-wrap: wrap; /* 设置换行 */
    > * {
      width: 49%;
    }
  }
</style>
